package cn.appsys.service.impl;

import cn.appsys.mapper.InfoMapper;
import cn.appsys.mapper.VersionMapper;
import cn.appsys.pojo.Info;
import cn.appsys.pojo.Version;
import cn.appsys.service.InfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class InfoServiceImpl implements InfoService {

    @Resource
    InfoMapper infoMapper;
    @Resource
    VersionMapper versionMapper;

    @Override
    public List<Info> getAll(String softwareName, Long status, Long flatformId, Long devId, Long categoryLevel1, Long categoryLevel2, Long categoryLevel3, Integer offset, Integer pageSize) {
        return infoMapper.getAll(softwareName, status, flatformId, devId, categoryLevel1, categoryLevel2, categoryLevel3, offset, pageSize);
    }

    @Override
    public Integer getCount(String softwareName, Long status, Long flatformId, Long devId, Long categoryLevel1, Long categoryLevel2, Long categoryLevel3) {
        return infoMapper.getCount(softwareName, status, flatformId, devId, categoryLevel1, categoryLevel2, categoryLevel3);
    }

    @Override
    public Integer addInfo(Info info) {
        return infoMapper.addInfo(info);
    }

    @Override
    public Info getInfoById(Long id) {
        return infoMapper.getInfoById(id);
    }

    @Override
    public Info checkApkName(String apkName) {
        return infoMapper.checkApkName(apkName);
    }

    @Override
    public Integer updateInfo(Info info) {
        return infoMapper.updateInfo(info);
    }

    @Override
    public Integer updatePicBeNull(Long id, String logoLocPath) throws Exception {
        Integer result = null;
        result = infoMapper.updatePicBeNull(id);
        if (result > 0) {
            new File(logoLocPath).delete();
        }
        return result;
    }

    @Override
    public Integer updateVersionId(Long id, Long versionId) {
        return infoMapper.updateVersionId(id, versionId);
    }

    @Override
    public Integer delInfo(Long id, List<Version> versionList) throws Exception {
        Integer num = 0;
        if (versionMapper.delVersionByAppId(id) > 0) {
            num = infoMapper.delInfo(id);
            for (Version version : versionList) {
                new File(version.getDownloadLink()).delete();
            }
        }
        return num;
    }

    @Override
    public Integer updateStatus(Long id, Long status) {
        return infoMapper.updateStatus(id, status);
    }
}
